package main

import (
	"github.com/veandco/go-sdl2/sdl"
)

const (
	W = 670
	H = 520
)

var (
	Gwindow *sdl.Window
	Grender *sdl.Renderer
	texture *Texture
)

func init() {
	sdl.Init(sdl.INIT_EVERYTHING)
	Gwindow, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, W, H, sdl.WINDOW_SHOWN)
	if err != nil {
		panic(err)
	}
	Grender, err = sdl.CreateRenderer(Gwindow, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		panic(err)
	}
	texture = &Texture{}
	texture.LoadFromFile("../asserts/image/arrow.png")
}

func main() {
	flipType := sdl.FLIP_NONE
	degree := float64(0)

	running := true
	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				println("Quit")
				running = false
				break
			//case *sdl.KeyboardEvent:
			case *sdl.KeyboardEvent:
				if t.State == sdl.PRESSED {
					switch t.Keysym.Sym {
					case sdl.K_a:
						degree -= 60
					case sdl.K_d:
						degree += 60
					case sdl.K_q:
						flipType = sdl.FLIP_HORIZONTAL
					case sdl.K_w:
						flipType = sdl.FLIP_NONE
					case sdl.K_e:
						flipType = sdl.FLIP_VERTICAL
					}
				}
			}
		}
		Grender.SetDrawColor(128, 128, 128, 255)
		Grender.Clear()
		dst := &sdl.Rect{(W - texture.GetW()) / 2, (H - texture.GetH()) / 2, 100, 100}
		texture.Render(nil, dst, degree, nil, flipType)

		Grender.Present()
		sdl.Delay(16)
	}
	free()
}

func free() {
	texture.Free()
	Grender.Destroy()
	Gwindow.Destroy()
	sdl.Quit()
}
